#include <iostream>
using namespace std;

/**
 * 双向链表
 */

typedef struct Node{
    int data;
    struct Node *prior,*next;
}LinkNode;

typedef struct Node *DoubleLink;

void CreateDoubleLink(DoubleLink *doubleLink){
    (*doubleLink) = (DoubleLink)malloc(sizeof(LinkNode));
    (*doubleLink)->next = NULL;
    (*doubleLink)->prior = NULL;
}

// 头插法
void InsertHead(DoubleLink *doubleLink,int data) {
    DoubleLink newNode = (DoubleLink)malloc(sizeof(LinkNode));
    newNode->data = data;

    DoubleLink oldNextContent;
    oldNextContent = (*doubleLink)->next;

    if (oldNextContent == NULL) {
        (*doubleLink)->next = newNode;
        newNode->next = oldNextContent;
        newNode->prior = (*doubleLink);
    } else {
        (*doubleLink)->next->prior = newNode;
        (*doubleLink)->next = newNode;
        newNode->next = oldNextContent;
        newNode->prior = (*doubleLink);
    }

}

// 尾插法
void InsertTail(DoubleLink *doubleLink,int data) {
    DoubleLink newNode = (DoubleLink)malloc(sizeof(LinkNode));
    newNode->data = data;

    DoubleLink Cursor;
    Cursor = (*doubleLink)->next;
    if (Cursor == NULL) {
        newNode->prior = (*doubleLink);
        (*doubleLink)->next = newNode;
    } else {
        for (;;) {
            if (Cursor->next == NULL) {
                Cursor->next = newNode;
                newNode->prior = Cursor;
                return;
            }
            Cursor = Cursor->next;
        }
    }

}

DoubleLink getNode(DoubleLink doubleLink,int i) {
    DoubleLink cursor;
    cursor = doubleLink->next;
    for (int j = 0; j < i; ++j) {
        cursor = cursor->next;
        if (cursor == NULL) {
            return NULL;
        }
    }
    return cursor;
}

void PrintList(DoubleLink linkList) {
    DoubleLink cursor;
    cursor = linkList->next;
    while (cursor) {
        printf("%d",cursor->data);
        cursor = cursor->next;
    }
}

int main() {
    DoubleLink doubleLink;
    CreateDoubleLink(&doubleLink);
    InsertTail(&doubleLink,1);
    InsertTail(&doubleLink,2);
    InsertTail(&doubleLink,3);
    InsertTail(&doubleLink,4);
    InsertTail(&doubleLink,5);
    PrintList(doubleLink);
}



